const uploadFile = async (path, size, uploadUrl) => {
  // 第一步 获取 hash 值
  let hash = await new Promise((resolve, reject) => {
    uni.getFileInfo({
      filePath: path,
      success: (result) => {
        resolve(result.digest);
      },
    });
  });
  // 第二步 请求获取基础文件信息
  let [aerror, ares] = await uni.request({
    url: uploadUrl,
    method: "POST",
    header: {
      "content-type": "application/json",
      Accept: "application/json",
      Authorization: "Bearer " + uni.getStorageSync("accessToken").data,
    },
    data: {
      filename: path,
      hash: hash,
      mime_type: "video/" + path.split(".")[path.split(".").length - 1],
      size: size,
      storage: {
        channel: "public",
      },
    },
  });
  console.log(aerror);
  console.log(ares);

  // 第三步 将文件以buffer流方式存入系统
  ares.data.headers.Accept = "application/json";
  let [berror, bres] = await uni.request({
    url: ares.data.uri,
    method: "PUT",
    header: ares.data.headers,
    node: ares.data.node,
    form: ares.data.form,
    file_key: ares.data.file_key,
    data: uni.getFileSystemManager().readFileSync(path),
  });
  console.log(berror);
  console.log(bres);
  return bres;
};
export default uploadFile;
